﻿using System;
using System.Linq;
using System.Windows.Forms;
using agsXMPP;
using Lapd.Common.CustomStanzas;
using agsXMPP.protocol.client;
using System.Xml.Linq;
using System.Collections.Generic;
using Lapd.Common.Entities;
using Lapd.Common.Helpers;

namespace Lapd.PoC
{
    public partial class ListenFromTopic : Form
    {
        private XmppClientConnection xmppClientConnection;

        public ListenFromTopic(XmppClientConnection xmppClientConnection)
        {
            InitializeComponent();

            this.xmppClientConnection = xmppClientConnection;

            this.xmppClientConnection.OnIq += new IqHandler(xmppClientConnection_OnIq);
            this.xmppClientConnection.OnMessage += new MessageHandler(xmppClientConnection_OnMessage);
        }

        void xmppClientConnection_OnMessage(object sender, Message msg)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new MessageHandler(xmppClientConnection_OnMessage), new object[] { sender, msg });
                return;
            }
            
            // Parse XML string
            XElement messageTopicResponseDocument = XElement.Parse(msg.ToString());

            // Search <samplefield> elements
            XNamespace sddsNamespace = "sddsopenfire";
            IEnumerable<XElement> xmlSampleFields = from xmlSampleField in messageTopicResponseDocument.Descendants(sddsNamespace + "samplefield")
                                                    select xmlSampleField;

            // Parse and convert into samplefield objects
            IEnumerable<SampleFieldEntity> sampleFields = XmlParseHelper.SampleFieldXElementsToSampleFieldEntities(xmlSampleFields);

            // Display
            foreach (SampleFieldEntity sampleField in sampleFields)
            {
                txtWrittenOnTopic.Text += String.Format("DataType: {0}, Value: {1}", sampleField.DataType, sampleField.Value);
            }

        }

        void xmppClientConnection_OnIq(object sender, IQ iq)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new IqHandler(xmppClientConnection_OnIq), new object[] { sender, iq });
                return;
            }

            if (iq.Query != null && iq.Query.GetType() == typeof(Sdds))
            {
                // it is a Sdds query
                Sdds sdds = iq.Query as Sdds;
                if (iq.Type == IqType.set)
                {
                    txtWrittenOnTopic.Text += "Mensaje recibido ";
                    
                }
            }
        }

        private void MitListenClick(object sender, EventArgs e)
        {
            // Prepare SddsIq LISTEN packet
            Jid jidTo = new Jid(CustomStanzasConstants.JidToPreffixConst + "." + Session.Server);
            Jid jidFrom = new Jid(Session.Jid);

            Listen listen = new Listen(txtTopic.Text);
            
            Sdds sdds = new Sdds(listen);
            SddsIq sddsIq = new SddsIq(sdds);
            sddsIq.Type = IqType.set;
            sddsIq.To = jidTo;
            sddsIq.From = jidFrom;

            xmppClientConnection.Send(sddsIq);
        }

        private void MitCloseClick(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}